package soft.quicknote.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

import soft.quicknote.R;
import soft.quicknote.beans.Category;


class CatNameWatcher implements TextWatcher {
    int pos;
    ArrayList<Category> mList;

    public CatNameWatcher(ArrayList<Category> list) {
        mList = list;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        mList.get(pos).setName(s.toString());
    }

    public void updatePosition(int i) {
        pos = i;
    }
}

class CategoryViewHolder extends RecyclerView.ViewHolder {

    public CheckBox checkBox;
    //    EditText catname;
//    TextView id;
    Category mCategory;

    public CategoryViewHolder(View itemView) {
        super(itemView);
        checkBox = (CheckBox) itemView.findViewById(R.id.category_checkbox);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (mCategory == null) return;
                mCategory.setChecked(isChecked);
            }
        });
//        catname = (EditText) itemView.findViewById(R.id.category_name);
//        id = (TextView) itemView.findViewById(R.id.category_id);
//        catname.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (mCategory == null) return;
//                mCategory.setName(s.toString());
//            }
//        });
    }


    public void bindCategory(Category cat) {
        mCategory = cat;
        checkBox.setText(cat.getName());
    }

}

public class CategoryAdapter extends RecyclerView.Adapter<CategoryViewHolder> {


    public ArrayList<Category> mList;
    final Context mContext;

    public int selectedPosition = -1;

    public CategoryAdapter(Context context, ArrayList<Category> cats) {
        mList = cats;
        mContext = context;
    }

    @NonNull
    @Override
    public CategoryViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_category, viewGroup, false);
        final CategoryViewHolder holder = new CategoryViewHolder(view);

        view.findViewById(R.id.edit_cat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = LayoutInflater.from(mContext);
                final View view = inflater.inflate(R.layout.edit_category, null);
                final TextView textView = (TextView) view.findViewById(R.id.category_name_result);
                textView.setText(holder.checkBox.getText());
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("编辑分类名");
                builder.setView(view);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        holder.mCategory.setName(textView.getText().toString());
                        holder.bindCategory(holder.mCategory);
                        dialog.dismiss();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create().show();
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull CategoryViewHolder holder, int i) {
        holder.bindCategory(mList.get(i));
        if (i == selectedPosition) {
            holder.itemView.setSelected(true);
        } else {
            holder.itemView.setSelected(false);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


}
